package com.unitechs.trap.excel.webapi.service.impl.entity;

import com.unitechs.trap.common.pojo.trap.entity.TrapVarName;
import com.unitechs.trap.common.pojo.trap.entity.TrapVarValue;
import com.unitechs.trap.common.pojo.trap.entity.VarData;
import com.unitechs.trap.excel.service.ITrapVarValueExcelService;

import com.unitechs.trap.excel.webapi.mapper.ReadAlaOutMapper;
import com.unitechs.trap.excel.webapi.mapper.VarDataMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;


@DubboService
@Slf4j
@Service
public class TrapVarValueExcelServiceImpl implements ITrapVarValueExcelService {
    @Autowired
    VarDataMapper varDataMapper;
    @Override
    public List<TrapVarValue> listTrapVarValue() {
        List<VarData> allData = varDataMapper.listVarData();
        allData=deduplicationWithJDBC(allData);
        List<TrapVarValue> trapVarValues=new ArrayList<>();
        allData.forEach(data->{
            TrapVarValue trapVarValue=new TrapVarValue();
            trapVarValue.setVarOid(data.getVarOid());
            trapVarValue.setVarValue(data.getVarValue());
            trapVarValue.setValueDesc(data.getVarDesc());
            trapVarValue.setTid(UUID.randomUUID().toString());
            LocalDateTime localDateTime=LocalDateTime.now();
            trapVarValue.setCreateTime(localDateTime);
            trapVarValue.setModifyTime(localDateTime);
            if (trapVarValue.getVarValue()==null){
            }else {
            trapVarValues.add(trapVarValue);}
        });
        return trapVarValues;
    }

    private List<VarData> deduplication(List<VarData> varData){
        Set<String> uniqueOids = new HashSet<>();
        List<VarData> filteredList = new ArrayList<>();
        for (VarData data : varData) {
            String a=data.getVarOid()+data.getVarValue();
            if (uniqueOids.add(a)) {
                filteredList.add(data);
            }

        }
        return filteredList;
    }

    @Autowired
    private ReadAlaOutMapper mapper;
    private List<VarData> deduplicationWithJDBC(List<VarData> varData){
        List<TrapVarValue> trapVarValues = mapper.listTrapVarValue();
        Set<String> uniqueOids = new HashSet<>();

        for (TrapVarValue trapVarValue:trapVarValues){
            uniqueOids.add(trapVarValue.getVarOid()+trapVarValue.getVarValue());
        }

        List<VarData> filteredList = new ArrayList<>();
        for (VarData s : varData) {
            String a=s.getVarOid()+s.getVarValue();
            if (uniqueOids.add(a)) {
                filteredList.add(s);
            }

        }
        return filteredList;

    }
}
